package core

import (
	"fmt"
	"gin-demo/sysinit"
	"os"
	"path"
	"time"
)

// Model 基础模型
type Model struct {
	ID        uint       `json:"id" gorm:"primary_key"`
	CreatedAt time.Time  `json:"created_at"`
	UpdatedAt time.Time  `json:"updated_at"`
	DeletedAt *time.Time `json:"deleted_at" sql:"index"`
}

// Upload 文件上传模型
type Upload struct {
	Model
	Name string `json:"name" sql:"index"`
	Path string `json:"path"`
}

// FullPath 获取文件全路径
func (f Upload) FullPath() string {
	return path.Join(sysinit.Config.GetString("path.upload"), f.Path)
}

// SetPath 设置文件上传路径
func (f Upload) SetPath() {
	for {
		f.Path = fmt.Sprintf("%d.%s", time.Now().UnixNano()/1000, path.Ext(f.Name))
		if _, err := os.Stat(f.FullPath()); os.IsNotExist(err) {
			break
		}
		f.Path = ""
	}
}
